var SignProject = function(){
	
	var ps = new ProjectSource();
	
	var projectCode = null;
	var project_id ='-1';
	//下载 文件
	var downloadUrl = function(s, m, rec){
		return "<a href='../task/downloadFile.form?fileId="+s+"'>下载</a>";
	};
	//附件列表
	var fileStore = new Ext.data.JsonStore(
			{
				url : '../task/searchTaskUpLoadFile.form?timesnap='+new Date().getTime(),
				baseParams: {exeId: '-1'},
				root : 'result',
				fields: ['ACCESSORY_ID', 'ACCESSORY_NAME', 'UPLOAD_DATE','FILE_SIZE','UPLOAD_NAME','ACCESSORY_DESC','USER_NAME'],
				listeners : {
					scope : this,
					beforeload : function(thiz) {
						thiz.baseParams = { exeId : project_id };
					}
				}
			});
	var fileGrid = new Ext.grid.GridPanel({
		width : 900,
		height : 150,
		store : fileStore,
		columns : [{header:'附件名称',width: 150,dataIndex:'ACCESSORY_NAME'},
		           {header:'上传日期',width: 150,dataIndex:'UPLOAD_DATE'},
		           {header:'文件大小(K)',width: 250,dataIndex:'FILE_SIZE'},
		           {header:'上传人',width: 250,dataIndex:'USER_NAME'},
		           {header: '操作',width: 50, dataIndex: 'ACCESSORY_ID',renderer:downloadUrl}
		           ],
   		bbar:new Ext.PagingToolbar({
   		   pageSize: 5,
            store: fileStore,
            displayInfo: true,
            displayMsg: '显示第 {0} 条到 {1} 条记录，一共 {2} 条',
            emptyMsg: "没有记录..."
           })
	});
	var fileUploadInfo = new Ext.form.FieldSet({
		title : '附件信息',
		collapsible : true,
		autoHeight : true,
		width : 930, 
		items : [fileGrid]
	});
	var signProjectStore = new Ext.data.JsonStore(
			{
				url : '../project/searchProjects.form?status=11&timesnap=' + new Date().getTime(),
				root : 'result',
				fields : [ 'PROJECT_ID', 'PROJECT_NAME', 'AREA_ID',
						'PROJECT_TYPE', 'EST_AMT', 'PROJECT_CREATE_DT',
						'EST_DT', 'JOIN_LEADER', 'CUSTOMER_NAME', 'STAFF_NUM',
						'CONTACT_PERSON', 'OFF_TEL', 'MOBILE', 'EMAIL',
						'PROJECT_TARGET', 'CUST_REQ', 'SERVICE_REQ',
						'IMP_FUNC', 'OWNER_PROVIDE', 'REMARK', 'COMPETITOR',
						'PROJECT_OWNER', 'INVOLVE_DT', 'ADVANTAGE',
						'DISADVANTAGE', 'CREATE_USER', 'CREATE_DT', 'MOD_USER',
						'MOD_DT' ],
			listeners : {
				scope : this,
				beforeload : function(thiz){
					thiz.baseParams = {
					searchName : searchName.getValue(),
					searchArea : searchArea.getValue(),
					searchComplateDate:searchComplateDate.getValue()?searchComplateDate.getValue().format('Y-m-d'):''
				};
			}
			}
			});	
	
	var spGrid = new Ext.grid.GridPanel({
		width : 930,
		height : 150,
		store : signProjectStore,
		listeners : {
			scope : this,
			rowmousedown : function(grid,index,e){
				var data = grid.store.getAt(index).data; 
				projectCode = data.PROJECT_ID;
				ps.setCompValueAndDisabled(data);
				ps.projectIsVs.setValue(true);
				project_id =data.PROJECT_ID;
				//项目相关附件
				fileStore.reload({params:{exeId:data.PROJECT_ID}});
			}
		},
		columns : [{header:'项目编号',width: 150,dataIndex:'PROJECT_ID'},
		           {header:'项目名称',width: 100, sortable: true,dataIndex:'PROJECT_NAME'},
		           {header:'所属区域',dataIndex:'AREA_ID',renderer:function(value,row){return ps.getGridAreaMap()[value];}},
		           {header:'项目类型',renderer:ps.renderprojectType,dataIndex:'PROJECT_TYPE'},
		           {header:'业务提供',width: 170,dataIndex:'OWNER_PROVIDE'},
		           {header:'开始时间',dataIndex:'PROJECT_CREATE_DT'},
		           {header:'计划结束时间',dataIndex:'EST_DT'},
		           {header:'估算金额',renderer: 'usMoney',dataIndex:'EST_AMT'}],
       bbar:new Ext.PagingToolbar({
  		 pageSize: 5,
           store: signProjectStore,
           displayInfo: true,
           displayMsg: '显示第 {0} 条到 {1} 条记录，一共 {2} 条',
           emptyMsg: "没有记录..."
         })
	});
	
	// 查询条件---begin
	var searchName = new Ext.form.TextField({
		fieldLabel : '项目名称',
		width : 180
	});
	
	var searchArea = new Ext.form.ComboBox( {
		fieldLabel : '归属区域',
		//hiddenName : 'type',
		store :ps.getGridArea ,
		mode : 'local',
		displayField : 'NAME',
		triggerAction : 'all',
		editable : false,
		valueField : 'AREA_ID',
		typeAhead : true,
		width : 150,
		emptyText : '---请选择---',
		selectOnFocus : true
	});
	
	var searchComplateDate = new Ext.form.DateField( {
		fieldLabel : '竣工时间',
		format : 'Y-m-d',
		width : 150
	});
	
	var btn_search = new Ext.Button({
		text : '查询',
		iconCls: 'icon-search',
		handler: function (){searchSignProject();}
	});
	// 查询条件---end

	// 签约信息---begin
	var fileUpload = new Ext.form.FileUploadField({
		fieldLabel : '上传附件',
		emptyText: '请选择要上传的文件',
		width : 520,
		name : 'fileName',
		buttonText: '',
		buttonCfg: {
            iconCls: 'icon-compress'
        }
	});
	var projectStatus = new Ext.form.RadioGroup({
		fieldLabel : '项目状态',
		name:'projectStatus',
		columns : 4,
		width : 300,
		listeners : {
			change : function(thiz,newValue,oldValue){
				signInfo.removeAll();
				var nv = newValue.boxLabel;
				if(nv == '已签约'){signInfo.add({layout:'column',
					  defaults:{layout:'form',columnWidth:0.3},
					  items:[{items:[{xtype:'datefield',
						  			  fieldLabel : '签约时间',
						  			  name:'signTime',
						  			  format : 'Y-m-d',
						  			  width : 150}]},
						  	 {items:[{xtype:'numberfield',
						  		 	  name : 'income',
					  			   fieldLabel:'预计收入',
					  			   width:150}]},
					  	     {items:[{xtype:'numberfield',
					  	    	 	  name :'exeCycle',
					  			   fieldLabel:'实施周期',
					  			   width:150}]}
						  			]});
		signInfo.add({xtype:'textarea',
					  width:600,
					  	name: 'payCondition',
					  fieldLabel:'付款条件'});
		signInfo.add(fileUpload);
		signInfo.doLayout();   }
				else if(nv == '未签约'){
					signInfo.add({xtype:'textarea',
								   name:'remark',
								  fieldLabel : '备注',
								  width : 800});
					signInfo.doLayout();
				}
				else if(nv == '丢单'){
					signInfo.add({layout:'column',
								  defaults:{layout:'form',columnWidth:0.5},
								  items:[{items:[{xtype:'datefield',name:'signTime2',fieldLabel:'对手签约时间',width:150,format:'Y-m-d'}]},
								         {items:[{xtype:'numberfield',name:'pactMoney',fieldLabel:'合同金额',width:150}]}]});
					signInfo.add({xtype:'textarea',name:'loseReason',fieldLabel:'丢单原因',width:700});
					signInfo.doLayout();
				}else if(nv == '项目取消'){
					signInfo.add({xtype:'textarea',
						        name: 'remark',
						  fieldLabel : '备注',
						  width : 800});
					signInfo.doLayout();
				}
			}
		},
		items : [{xtype:'radio',boxLabel:'已签约',checked:true,name:'signStatus',inputValue:'21'},
		         {xtype:'radio',boxLabel:'未签约',name:'signStatus',inputValue:'22'},
		         {xtype:'radio',boxLabel:'丢单',name:'signStatus',inputValue:'23'},
		         {xtype:'radio',boxLabel:'项目取消',name:'signStatus',inputValue:'24'}]
	});
	
	var signInfo = new Ext.form.FieldSet({
		title : '签约信息',
		collapsible : true,
		autoHeight : true,
		width : 930, 
		items : [],
		buttons : [{text:'保存',iconCls: 'icon-accept',scope:this,handler:function(){saveSign(this.spPanel);}},{text:'取消'}]
	});
	// 签约信息---end
	
	this.spPanel = new Ext.form.FormPanel({
		frame : true,
		width : 950,
		fileUpload: true, 
		labelWidth : 85,
		items : [{layout:'column',defaults:{layout:'form'},
			      items:[{columnWidth:0.3,items:[searchName]},
			             {columnWidth:0.3,items:[searchArea]},
			             {columnWidth:0.3,items:[searchComplateDate]},
			             {columnWidth:0.1,items:[btn_search]}]
				},spGrid,projectStatus,signInfo,ps.projectInfo,
				ps.projectNeed,ps.projectIsVs,ps.projectVsInfo,fileUploadInfo],
		listeners:{
	    	render: {fn:function(){ 
				ps.getGridArea.load();
				signProjectStore.load();
				//strackStore.load();
			signInfo.add({layout:'column',
						  defaults:{layout:'form',columnWidth:0.3},
						  items:[{items:[{xtype:'datefield',
							  			  fieldLabel : '签约时间',
							  			  name:'signTime',
							  			  format : 'Y-m-d',
							  			  width : 150}]},
							  	 {items:[{xtype:'numberfield',
							  		 	  name : 'income',
						  			   fieldLabel:'预计收入',
						  			   width:150}]},
						  	     {items:[{xtype:'numberfield',
						  	    	 	  name :'exeCycle',
						  			   fieldLabel:'实施周期',
						  			   width:150}]}
							  			]});
			signInfo.add({xtype:'textarea',
						  width:600,
						  	name: 'payCondition',
						  fieldLabel:'付款条件'});
			signInfo.add(fileUpload);
			signInfo.doLayout();                        
		
			}}
		}
	});
	var searchSignProject = function(){
		signProjectStore.reload();
	};
	var saveSign = function(sp){

		if(projectCode!=''&projectCode!=null){
			if(!sp.getForm().isValid()){
				Utils.info("有必填项没有填写，请检查！","系统提示");
				return false;
			}else{
				sp.getForm().submit({
					url:'../project/saveSignInfo.form',
					method:'post',
					params: {
						project_id:projectCode,
						fileName: fileUpload.getValue()
					},
					success:function(form,resp){
						Utils.info("操作成功","系统提示");	
						sp.getForm().reset();
						project_id ='-1';
						signProjectStore.reload();
						fileStore.reload({params:{exeId:'-1'}});
					}
				});
			}
		}else{
			Utils.info("请选择一个项目！","系统提示");
			return false;			
		}
		
	};
	
};